using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.IO;
using System.Globalization;
using System.Text.RegularExpressions;

namespace Utils
{
#if (!WIN_FORMS)
   public static class URLUtils
   {
      /// <summary>
      /// Returns http://domain.com:8080/ if HttpContext and Request exist; otherwise Empty string
      /// </summary>
      /// <returns></returns>
      public static string GetAbsVirtualAppPath()
      {
         if (HttpContext.Current != null && HttpContext.Current.Request != null)
         {
            HttpRequest request = HttpContext.Current.Request;
            StringBuilder path = new StringBuilder(request.Url.Scheme);
            path.Append("://");
            path.Append(request.Url.Host);
            if (request.Url.Port != 80)
            {
               path.Append(':');
               path.Append(request.Url.Port.ToString());
            }
            path.Append(
               HttpRuntime.AppDomainAppVirtualPath.EndsWith("/") ? 
                  HttpRuntime.AppDomainAppVirtualPath : 
                  String.Format("{0}/", HttpRuntime.AppDomainAppVirtualPath));
            return path.ToString();
         }
         else
         {
            return string.Empty;
         }
      }
      /// <summary>
      /// Returns correct resolved URL even when app uses cookieless session (with /S(skjskdjfksdf) in URL)
      /// </summary>
      /// <param name="unmappedUrl"></param>
      /// <returns></returns>
      public static string ToAbsolutePath(string unmappedUrl)
      {
         string appAbsPath = GetAbsVirtualAppPath();
			if (String.IsNullOrEmpty(appAbsPath))
				return unmappedUrl;

         string s = unmappedUrl.Replace("~/", appAbsPath.ToString());
         return s;
      }
			

      public static string ToRelativePath(string virtualPath)
      {
         if (HttpContext.Current != null && HttpContext.Current.Request != null)
         {
            string appAbsPath = GetAbsVirtualAppPath();
            if (virtualPath.StartsWith("http://", true, CultureInfo.InvariantCulture))
            {
               return VirtualPathUtility.ToAppRelative(virtualPath.Replace(appAbsPath, "/"));
            }
         }
         return virtualPath;
      }

      /// <summary>
      /// Creates virtual path from physical one
      /// </summary>
      /// <param name="physPath"></param>
      /// <returns></returns>
      public static string ToVirtualPath(string physPath)
      {
			if (!(HttpContext.Current != null && HttpContext.Current.Request != null))
				return physPath;

         if (physPath.Substring(1).StartsWith(":\\"))
         {
            //return VirtualPathUtility.ToAppRelative(physPath.Replace(HttpRuntime.AppDomainAppPath, HttpRuntime.AppDomainAppVirtualPath + "/").Replace('\\', '/'));
				return VirtualPathUtility.ToAppRelative(physPath.Replace(HttpRuntime.AppDomainAppPath, HttpRuntime.AppDomainAppVirtualPath + "/").Replace('\\', '/'));
         }
         else
         {
            return physPath;
         }
      }

      public static string ResolveUrl(string relativeUrl)
      {
			if (HttpContext.Current != null && HttpContext.Current.Request != null)
			{
				HttpRequest request = HttpContext.Current.Request;
				string s = relativeUrl.Replace("~", request.ApplicationPath);
				return s;
			}
			else
				return relativeUrl;
      }

      /// <summary>
      /// Converts relative virtual path to absolute local (~/images/image.jpg -> E:\images\image.jpg)
      /// </summary>
      /// <param name="relativeUri"></param>
      /// <returns></returns>
      public static string ToLocalPath(string relativeUri)
      {
			if (System.AppDomain.CurrentDomain == null)	// might be design-time
				return relativeUri;

         return Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, relativeUri.Replace("~/", string.Empty)).Replace('/', '\\');
      }
   }
#endif
}
